package com.spukmk2me.spukmk2mesceneeditor.gui;

import javax.swing.JOptionPane;

public class ScreenSizeDialog extends javax.swing.JDialog
{
    public ScreenSizeDialog( java.awt.Frame parent, boolean modal,
        SceneDisplaySurface displayedSurface ) {
        super( parent, modal );
        initComponents();
        m_displayedSurface = displayedSurface;
        importData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        m_widthTextField = new javax.swing.JTextField();
        m_heightTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        m_originXTextField = new javax.swing.JTextField();
        m_originYTextField = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Preview screen size");

        jLabel1.setText("Screen width:");

        jLabel2.setText("Screen height:");

        jLabel3.setText("Origin X:");

        jLabel4.setText("Origin Y:");

        m_originXTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_originXTextFieldActionPerformed(evt);
            }
        });

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(m_heightTextField)
                    .addComponent(m_widthTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(m_originXTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(m_originYTextField)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(185, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(m_widthTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(m_originXTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(m_heightTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(m_originYTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void m_originXTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_originXTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_m_originXTextFieldActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try
        {
            int width   = Integer.parseInt( m_widthTextField.getText() );
            int height  = Integer.parseInt( m_heightTextField.getText() );
            int originX = Integer.parseInt( m_originXTextField.getText() );
            int originY = Integer.parseInt( m_originYTextField.getText() );

            if ( width <= 0 )
            {
                JOptionPane.showMessageDialog( this,
                    "Invalid screen width", "Error",
                    JOptionPane.ERROR_MESSAGE );
                return;
            }

            if ( width > 16383 )
            {
                JOptionPane.showMessageDialog( this,
                    "Screen width is too large", "Error",
                    JOptionPane.ERROR_MESSAGE );
                return;
            }

            if ( height <= 0 )
            {
                JOptionPane.showMessageDialog( this,
                    "Invalid screen height", "Error",
                    JOptionPane.ERROR_MESSAGE );
                return;
            }

            if ( height > 16383 )
            {
                JOptionPane.showMessageDialog( this,
                    "Screen height is too large", "Error",
                    JOptionPane.ERROR_MESSAGE );
                return;
            }

            m_displayedSurface.setOrigin( originX, originY );
            m_displayedSurface.setDisplayedSize( width, height );
            this.dispose();
        } catch ( NumberFormatException e ) {
            JOptionPane.showMessageDialog( this,
                "Invalid number", "Error",
                JOptionPane.ERROR_MESSAGE );
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void importData()
    {
        m_widthTextField.setText(
            String.valueOf( m_displayedSurface.getDisplayedWidth() ) );
        m_heightTextField.setText(
            String.valueOf( m_displayedSurface.getDisplayedHeight() ) );
        m_originXTextField.setText(
            String.valueOf( m_displayedSurface.getOriginX() ) );
        m_originYTextField.setText(
            String.valueOf( m_displayedSurface.getOriginY() ) );
    }

    private SceneDisplaySurface m_displayedSurface;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField m_heightTextField;
    private javax.swing.JTextField m_originXTextField;
    private javax.swing.JTextField m_originYTextField;
    private javax.swing.JTextField m_widthTextField;
    // End of variables declaration//GEN-END:variables

}
